<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Packages;
use app\common\model\UserPackagesConfig;
use fast\Random;

/**
 * 开卡
 */
class Activatecard extends Api
{
    protected $noNeedLogin = ['address','haoma','orderSync','getPhonePool'];
    protected $noNeedRight = ['*'];

    public function address(){
        $data['appid']     = config('site.ooen_appid');
        $data['timestamp'] = time();
        $data['nonce_str'] = Random::alnum(32);
        $params            = input('post.');

        $info = Packages::where(['id'=>$params['id'],'status'=>1])->find();
        if(!$info){
            $this->error('产品不存在或已下架');
        }
        $params['id']      = $info['packages_id'];
        $data              = array_merge($data,$params);
        $string            = ascll($data);
        $stringSignTemp  = $string.'&appsecret='.config('site.open_appsecret');;
        $data['sign']    = mb_strtoupper( md5($stringSignTemp));
        $url = "http://agent.haoka.bx10086.cn/v1/haoka_api/area";//获取地区
//        $url = "http://agent.haoka.bx10086.cn/v1/haoka_api/packageOrder";//办理套餐
        $res = httpPost($url,$data);
        $arr = json_decode($res,true);
        if(empty($arr['area'])){
            $this->error('套餐不存在');
        }
        $this->success('ok',$arr['area']);
    }

    public function haoma(){
        $cardType = input('post.cardType');
        if(!$cardType){
            $this->error('缺少参数');
        }
        $provinceName = input('post.provinceName');
        if(!$provinceName){
            $this->error('缺少参数');
        }
        $cityName = input('post.cityName');
        if(!$cityName){
            $this->error('缺少参数');
        }
        $provinceName  =str_replace('省','',$provinceName);
        $info = Packages::where(['id'=>(int)$cardType,'status'=>1])->find();
        if(!$info){
            $this->error('产品不存在或已下架');
        }
        $searchValue = input('post.searchValue');
        if($info['platform_type'] == 2){
            $params['partnerId']    = '33587';
            $secret                 = "kIVgqdLiNGYP3GeM";
            $url                    = "http://hao.san-jk.com/selectNumber";
        }else{
            $params['partnerId']    = '25017';
            $secret                 = "lQiUQJsnOqIaBqoU";
            $url                    = "http://haoka.zhnxu.com/selectNumber";
        }
        //，343、345是海南三亚市。165是浙江宁波市
//        if($info['id']== 132 || $info['id'] == 146 || $info['id'] == 155 || $info['id'] == 181 || $info['id'] == 182){
//            $params['provinceName'] = config('site.lc_provinceName');
//            $params['cityName']     = config('site.lc_cityName');
//        }elseif($info['id']== 343 || $info['id']== 345){
//            $params['provinceName'] = "海南";
//            $params['cityName']     = "三亚市";
//        }elseif ($info['id']== 165){
//            $params['provinceName'] = "浙江";
//            $params['cityName']     = "宁波市";
//        }else{
            $params['provinceName'] = $provinceName;
            $params['cityName']     = $cityName;
//        }
//
//        if($info['id']== 371 ){
//            $params['provinceName'] = "湖南";//省
//            $params['cityName']     = "长沙市";//市
////            $params['postDistrictName'] = '';
//        }
//
//        if($info['id']== 440 ||  $info['id']== 441 ){
//            $params['provinceName'] = "广东";//省
//            $params['cityName']     = "广州市";//市
////            $params['postDistrictName'] = '';
//        }
        if($info['province_name']){
            $params['provinceName'] = $info['province_name'];//省
        }
        if($info['city_name']){
            $params['cityName']     = $info['city_name'];//市
        }
        $params['cardType']     = $info['packages_id'];
        if($searchValue){
            $params['searchValue'] = $searchValue;
        }
        $params['pageNo']          = input('post.pageNo',1);
        ksort($params);//升序排序
        $str = '';
        foreach ($params as $k => $v) {
            $str .=  $k  . $v;  //cardType4cityName郑州市pageNo1partnerId1proniceName河南searchValue4150
        }
        $str = $secret.$str.$secret;//abc123cardType4cityName郑州市pageNo1partnerId1proniceName河南searchValue4150222abc123
        $sign=strtoupper(md5($str)); //93932E8BD15A2EBE6D0C4F3890B97D90
        $params['sign'] = $sign;
//          dump($params['partnerId']);
//          dump($params);
        $res = json_decode(httpPost($url, $params),true);
        if(empty($res['data']['numArray'])){
            unset($params['sign']);
            $params['certName'] = input('certName');
            $params['certNo'] =  input('certNo');
            $params['contactNum'] = input('contactNum');
            ksort($params);//升序排序
            $str = '';
            foreach ($params as $k => $v) {
                $str .=  $k  . $v;  //cardType4cityName郑州市pageNo1partnerId1proniceName河南searchValue4150
            }
            $str = $secret.$str.$secret;
            $sign=strtoupper(md5($str));
            $params['sign'] = $sign;
            $res = json_decode(httpPost($url, $params),true);
//            dump($res);
        }

        if(!empty($res['data']['numArray'])){
            $res['data']['numArrayTwo'] = [];
            foreach ($res['data']['numArray'] as $k=>$v){
                $res['data']['numArrayTwo'][$k]['id']   = $k+1;
                $res['data']['numArrayTwo'][$k]['name'] = $v;
                if($res['data']['checkList']){
                    $res['data']['numArrayTwo'][$k]['code'] = $res['data']['checkList'][$k];
                }

            }
//              dump($res);
            $this->success('ok',$res);
        }else{

            $this->success('不能选号可直接提交');
        }

//         $this->error($res['info']);
    }

    /**
     * @menu:卡管家选号
     * @Author:男人的坏
     * @Date:2024-04-19 07:06
     */
    public function getPhonePool(){
        $ids = input('ids');
        $province = input('post.provinceName');
        if(!$province){
            $this->error('缺少参数');
        }
        $city = input('post.cityName');
        if(!$city){
            $this->error('缺少参数');
        }
        if($city == '市辖区'){
            $city = $province;
        }
        $info = Packages::where(['id'=>$ids,'status'=>1])->find();
        if(!$info){
            $this->error('产品不存在或已下架');
        }
        $path = '/api/card/open/getPhonePool';
        $res = kgjcurl($path, ['goodsCode'=>$info['packages_id'],'province'=>$province,'city'=>$city]);
        if($res['code'] !=200){
            $this->error($res['msg']);
        }
        $this->success('ok',$res['data']);
    }

}
